package net.siufung.core.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.Nonnull;

/**
 * @author 陈建峰
 * @date 2022/9/27 9:36 上午
 * @since V1.0.0
 */
public abstract class AbstractEventBus implements IEventBus, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(@Nonnull ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        this.scanConsumer(null);
    }

    @Override
    public void scanConsumer(String packageName) {
        applicationContext.getBeansOfType(IEventConsumer.class).forEach((k, v)->{
            this.addConsumer(v);
        });
    }
}
